<?

//Initiate Authenticate

if (!GetParam("bypasslogin","")) {
    SecurityRedirect ();
}
//initiate Vars 
define("PRIMARY_KEY","group_id");
$KEY[]         = array("PRIMARY_KEY"=>PRIMARY_KEY);
$FileName      = "ais_tools_backdata.php";
$FileTemplate  = "ais_tools_backdata_list.html";
$TableName     = "tblgroup";
$SubHeaderTitle= "Backup/Restore Management";
$HTMLFormName  = "BackupRestore";
$PageSize      = 10 ;
$HTMLGridList  = "GroupGrid";
//************ BACKUP VARIABLES ***********


//************ FORM ACTION **************
 if (GetParam("Action","")=="Delete") {
	 if (GetParam("FileName","")) {
		if (file_exists(BACKUP_DIR."/".GetParam("FileName",""))) {
			unlink(BACKUP_DIR."/".GetParam("FileName",""));
			header("Location: $FileName");
			exit;
		}
 	 }

 }elseif (GetParam("Action","")=="Restore") {
	 if (GetParam("FileName","")) {
		if (file_exists(BACKUP_DIR."/".GetParam("FileName",""))) {

			$ShellStr = "unzip -joqq ".BACKUP_DIR."/".GetParam("FileName","")." -d $TMP_DIR ";
			$output = shell_exec($ShellStr);

			$ShellStr = "mysql -u ".DB_USER." ".DB_NAME." < ".$TMP_DIR."/".ereg_replace("zip","sql",GetParam("FileName",""));			
			$output = shell_exec($ShellStr);
			//print $ShellStr;

			unlink($TMP_DIR."/".ereg_replace("zip","sql",GetParam("FileName","")));
			header("Location: $FileName");

			exit;
		}
 	 }

 }elseif (GetParam("Action","")=="Backup") {
	 if (GetParam("backuptype","")=="BD") {
		 $year = GetParam("periodYear","");
		 $month = GetParam("periodMonth","");

		 if (GetParam("fromDate","")) {
			list($day,$month,$year) = explode("/",GetParam("fromDate",""));
			$FDate = "$year-$month-$day";
		 	$Where = " tglbkt>='$FDate'";
		 	$WhereReceipt = " tglkwit>='$FDate'";
	 	 }

		 if (GetParam("toDate","")) {
			list($day,$month,$year) = explode("/",GetParam("toDate",""));
			$TDate = "$year-$month-$day";
		 	$Where .= " AND tglbkt<='$TDate'";
		 	$WhereReceipt .= " AND tglkwit<='$TDate'";
	 	 }

	 	$FName = $TMP_DIR."/Backup_PARTIAL_From_$FDate"."_To_"."$TDate".".sql";

        //DUMP TO TRANSFER DATA
		$ShellStr = "mysqldump --add-drop-table -w 'ledger=$year' -u ".DB_USER." ".DB_NAME." tblcoa > $FName";
		`$ShellStr`;
		$ShellStr = "mysqldump --add-drop-table -w \"$Where\" -u ".DB_USER." ".DB_NAME." ais_memorial >> $FName";
		`$ShellStr`;
		$ShellStr = "mysqldump --add-drop-table -w \"$Where\" -u ".DB_USER." ".DB_NAME." ais_piutang >> $FName";
		`$ShellStr`;
		$ShellStr = "mysqldump --add-drop-table -w \"$Where\" -u ".DB_USER." ".DB_NAME." ais_hutang >> $FName";
		`$ShellStr`;
		$ShellStr = "mysqldump --add-drop-table -w \"$Where\" -u ".DB_USER." ".DB_NAME." ais_advance >> $FName";
		`$ShellStr`;
		$ShellStr = "mysqldump --add-drop-table -w \"$WhereReceipt\" -u ".DB_USER." ".DB_NAME." ais_receipt >> $FName";
		`$ShellStr`;
		$ShellStr = "mysql -u ".DB_USER." ".TransDB." < $FName";
		$output = shell_exec($ShellStr);

		$ShellStr = "mysql -u ".DB_USER." -T -e 'ALTER TABLE ".TransDB.".ais_memorial DROP memorial_id'";
        $output = shell_exec($ShellStr);
		$ShellStr = "mysql -u ".DB_USER." -T -e 'ALTER TABLE ".TransDB.".ais_advance DROP adv_id'";
        $output = shell_exec($ShellStr);
		$ShellStr = "mysql -u ".DB_USER." -T -e 'ALTER TABLE ".TransDB.".ais_receipt DROP receipt_id'";
        $output = shell_exec($ShellStr);


        //DUMP TO BACKUP DATA
		`echo "DELETE FROM tblcoa WHERE ledger=$year ;\n" > $FName`;
		$ShellStr = "mysqldump -c -t -w 'ledger=$year' -u ".DB_USER." ".TransDB." tblcoa >> $FName";
		`$ShellStr`;

		`echo "DELETE FROM ais_memorial WHERE $Where ;\n" >> $FName`;
		$ShellStr = "mysqldump -c -t -w \"$Where\" -u ".DB_USER." ".TransDB." ais_memorial >> $FName";
		`$ShellStr`;

		`echo "DELETE FROM ais_piutang WHERE $Where ;\n" >> $FName`;
		$ShellStr = "mysqldump -c -t -w \"$Where\" -u ".DB_USER." ".TransDB." ais_piutang >> $FName";
		`$ShellStr`;

		`echo "DELETE FROM ais_hutang WHERE $Where ;\n" >> $FName`;
		$ShellStr = "mysqldump -c -t -w \"$Where\" -u ".DB_USER." ".TransDB." ais_hutang >> $FName";
		`$ShellStr`;

		`echo "DELETE FROM ais_advance WHERE $Where ;\n" >> $FName`;
		$ShellStr = "mysqldump -c -t -w \"$Where\" -u ".DB_USER." ".TransDB." ais_advance >> $FName";
		`$ShellStr`;

		`echo "DELETE FROM ais_receipt WHERE $WhereReceipt ;\n" >> $FName`;
		$ShellStr = "mysqldump -c -t -w \"$WhereReceipt\" -u ".DB_USER." ".TransDB." ais_receipt >> $FName";
		`$ShellStr`;

		 //ZIP Database
		 $ShellStr = "zip -jm ".BACKUP_DIR."/Backup_PARTIAL_From_$FDate"."_To_"."$TDate".".zip"." $FName";
		 `$ShellStr`;

		 header("Location: $FileName");
		 exit;

	 } elseif (GetParam("backuptype","")=="MD") {
		 if (GetParam("periodYear","")) {
			 $year = GetParam("periodYear","");
			 if (GetParam("periodMonth","")) {
				 $month=GetParam("periodMonth","");
				 if (GetParam("periodDay","")){
					 $Where = " year(tglbkt)=".GetParam("periodYear","")." AND month(tglbkt)=".GetParam("periodMonth","")." AND day(tglbkt)=".GetParam("periodDay","");
					 $WhereReceipt = " year(tglkwit)=".GetParam("periodYear","")." AND month(tglkwit)=".GetParam("periodMonth","")." AND day(tglbkt)=".GetParam("periodDay","");
				 	 $FName = $TMP_DIR."/Backup_PARTIAL_Daily_".GetParam("periodYear","")."-".GetParam("periodMonth","")."-".GetParam("periodDay","").".sql";
				 } else {
					 $Where = " year(tglbkt)=".GetParam("periodYear","")." AND month(tglbkt)=".GetParam("periodMonth","")." AND day(tglbkt)=".GetParam("periodDay","");
					 $WhereReceipt = " year(tglkwit)=".GetParam("periodYear","")." AND month(tglkwit)=".GetParam("periodMonth","")." AND day(tglbkt)=".GetParam("periodDay","");
				 	 $FName = $TMP_DIR."/Backup_PARTIAL_Period_".GetParam("periodYear","")."-".GetParam("periodMonth","").".sql";
			 	 }

                //DUMP TO TRANSFER DATA
        		$ShellStr = "mysqldump --add-drop-table -w 'ledger=$year' -u ".DB_USER." ".DB_NAME." tblcoa > $FName";
        		`$ShellStr`;
        		$ShellStr = "mysqldump --add-drop-table -w \"$Where\" -u ".DB_USER." ".DB_NAME." ais_memorial >> $FName";
        		`$ShellStr`;
        		$ShellStr = "mysqldump --add-drop-table -w \"$Where\" -u ".DB_USER." ".DB_NAME." ais_piutang >> $FName";
        		`$ShellStr`;
        		$ShellStr = "mysqldump --add-drop-table -w \"$Where\" -u ".DB_USER." ".DB_NAME." ais_hutang >> $FName";
        		`$ShellStr`;
        		$ShellStr = "mysqldump --add-drop-table -w \"$Where\" -u ".DB_USER." ".DB_NAME." ais_advance >> $FName";
        		`$ShellStr`;
        		$ShellStr = "mysqldump --add-drop-table -w \"$WhereReceipt\" -u ".DB_USER." ".DB_NAME." ais_receipt >> $FName";
        		`$ShellStr`;
                $ShellStr = "mysql -u ".DB_USER." ".TransDB." < $FName";
        		$output = shell_exec($ShellStr);

				$ShellStr = "mysql -u ".DB_USER." -T -e 'ALTER TABLE ".TransDB.".ais_memorial DROP memorial_id'";
		        $output = shell_exec($ShellStr);
				$ShellStr = "mysql -u ".DB_USER." -T -e 'ALTER TABLE ".TransDB.".ais_advance DROP adv_id'";
		        $output = shell_exec($ShellStr);
				$ShellStr = "mysql -u ".DB_USER." -T -e 'ALTER TABLE ".TransDB.".ais_receipt DROP receipt_id'";
		        $output = shell_exec($ShellStr);

//		        print $output;

                //DUMP TO BACKUP DATA
				 `echo "DELETE FROM tblcoa WHERE ledger=$year ;\n" > $FName`;
			 	 $ShellStr = "mysqldump -c -t -w 'ledger=$year' -u ".DB_USER." ".TransDB." tblcoa >> $FName";
				 `$ShellStr`;

				 `echo "DELETE FROM ais_memorial WHERE $Where ;\n" >> $FName`;				 
				 $ShellStr = "mysqldump -c -t -w \"$Where\" -u ".DB_USER." ".TransDB." ais_memorial >> $FName";
				 `$ShellStr`;

				 `echo "DELETE FROM ais_piutang WHERE $Where ;\n" >> $FName`;
			 	 $ShellStr = "mysqldump -c -t -w \"$Where\" -u ".DB_USER." ".TransDB." ais_piutang >> $FName";
				 `$ShellStr`;

				 `echo "DELETE FROM ais_hutang WHERE $Where ;\n" >> $FName`;
			 	 $ShellStr = "mysqldump -c -t -w \"$Where\" -u ".DB_USER." ".TransDB." ais_hutang >> $FName";
				 `$ShellStr`;

				 `echo "DELETE FROM ais_advance WHERE $Where ;\n" >> $FName`;
			 	 $ShellStr = "mysqldump -c -t -w \"$Where\" -u ".DB_USER." ".TransDB." ais_advance >> $FName";
				 `$ShellStr`;

				 `echo "DELETE FROM ais_receipt WHERE $WhereReceipt ;\n" >> $FName`;
			 	 $ShellStr = "mysqldump -c -t -w \"$WhereReceipt\" -u ".DB_USER." ".TransDB." ais_receipt >> $FName";
				 `$ShellStr`;

				//ZIP Database
				if (GetParam("periodDay","")){
					$ShellStr = "zip -jm ".BACKUP_DIR."/Backup_PARTIAL_Daily_".GetParam("periodYear","")."-".GetParam("periodMonth","")."-".GetParam("periodDay","").".zip"." $FName";
					`$ShellStr`;					
				}else {
					$ShellStr = "zip -jm ".BACKUP_DIR."/Backup_PARTIAL_Period_".GetParam("periodYear","")."-".GetParam("periodMonth","").".zip"." $FName";
					`$ShellStr`;
				}

		 	 } else {
			 	 $Where = " year(tglbkt)=".GetParam("periodYear","");
			 	 $WhereReceipt = " year(tglkwit)=".GetParam("periodYear","");
			 	 $FName = $TMP_DIR."/Backup_PARTIAL_Year_".GetParam("periodYear","").".sql";

                //DUMP TO TRANSFER DATA
        		$ShellStr = "mysqldump --add-drop-table -w 'ledger=$year' -u ".DB_USER." ".DB_NAME." tblcoa > $FName";
        		`$ShellStr`;
        		$ShellStr = "mysqldump --add-drop-table -w \"$Where\" -u ".DB_USER." ".DB_NAME." ais_memorial >> $FName";
        		`$ShellStr`;
        		$ShellStr = "mysqldump --add-drop-table -w \"$Where\" -u ".DB_USER." ".DB_NAME." ais_piutang >> $FName";
        		`$ShellStr`;
        		$ShellStr = "mysqldump --add-drop-table -w \"$Where\" -u ".DB_USER." ".DB_NAME." ais_hutang >> $FName";
        		`$ShellStr`;
        		$ShellStr = "mysqldump --add-drop-table -w \"$Where\" -u ".DB_USER." ".DB_NAME." ais_advance >> $FName";
        		`$ShellStr`;
        		$ShellStr = "mysqldump --add-drop-table -w \"$WhereReceipt\" -u ".DB_USER." ".DB_NAME." ais_receipt >> $FName";
        		`$ShellStr`;
        		$ShellStr = "mysql -u ".DB_USER." ".TransDB." < $FName";
        		$output = shell_exec($ShellStr);

				$ShellStr = "mysql -u ".DB_USER." -T -e 'ALTER TABLE ".TransDB.".ais_memorial DROP memorial_id'";
		        $output = shell_exec($ShellStr);
				$ShellStr = "mysql -u ".DB_USER." -T -e 'ALTER TABLE ".TransDB.".ais_advance DROP adv_id'";
		        $output = shell_exec($ShellStr);
				$ShellStr = "mysql -u ".DB_USER." -T -e 'ALTER TABLE ".TransDB.".ais_receipt DROP receipt_id'";
		        $output = shell_exec($ShellStr);

                //DUMP TO BACKUP DATA
				 `echo "DELETE FROM tblcoa WHERE ledger=$year ;\n" > $FName`;
			 	 $ShellStr = "mysqldump -c -t -w 'ledger=$year' -u ".DB_USER." ".TransDB." tblcoa >> $FName";
				 `$ShellStr`;

				 `echo "DELETE FROM ais_memorial WHERE $Where ;\n" >> $FName`;				 
				 $ShellStr = "mysqldump -c -t -w \"$Where\" -u ".DB_USER." ".TransDB." ais_memorial >> $FName";
				 `$ShellStr`;

				 `echo "DELETE FROM ais_piutang WHERE $Where ;\n" >> $FName`;
			 	 $ShellStr = "mysqldump -c -t -w \"$Where\" -u ".DB_USER." ".TransDB." ais_piutang >> $FName";
				 `$ShellStr`;

				 `echo "DELETE FROM ais_hutang WHERE $Where ;\n" >> $FName`;
			 	 $ShellStr = "mysqldump -c -t -w \"$Where\" -u ".DB_USER." ".TransDB." ais_hutang >> $FName";
				 `$ShellStr`;

				 `echo "DELETE FROM ais_advance WHERE $Where ;\n" >> $FName`;
			 	 $ShellStr = "mysqldump -c -t -w \"$Where\" -u ".DB_USER." ".TransDB." ais_advance >> $FName";
				 `$ShellStr`;

				 `echo "DELETE FROM ais_receipt WHERE $WhereReceipt ;\n" >> $FName`;
			 	 $ShellStr = "mysqldump -c -t -w \"$WhereReceipt\" -u ".DB_USER." ".TransDB." ais_receipt >> $FName";
				 `$ShellStr`;

				//ZIP Database
				$ShellStr = "zip -jm ".BACKUP_DIR."/Backup_PARTIAL_Year_".GetParam("periodYear","").".zip"." $FName";
				`$ShellStr`;
			 }
	 	 }

		header("Location: $FileName");
		exit;
	 } elseif (GetParam("backuptype","")=="FD") {
		$FName = $TMP_DIR."/Backup_ALLDATABASE_".date("Y-m-d").".sql";
		$ShellStr = "mysqldump --add-drop-table -u ".DB_USER." ".DB_NAME." > $FName";
		
		`$ShellStr`;
//die($ShellStr);
		//ZIP Database
		$ShellStr = "zip -jm ".BACKUP_DIR."/Backup_ALLDATABASE_".date("Y-m-d").".zip"." $FName";
		`$ShellStr`;

		header("Location: $FileName");
		exit;
	 } elseif (GetParam("backuptype","")=="FT") {
		$FName = $TMP_DIR."/Backup_ALLTRANSACTIONDB_".date("Y-m-d").".sql";
		$ShellStr = "mysqldump --add-drop-table -u ".DB_USER." ".DB_NAME." tblcoa > $FName";
		`$ShellStr`;
		$ShellStr = "mysqldump --add-drop-table -u ".DB_USER." ".DB_NAME." ais_memorial >> $FName";
		`$ShellStr`;
		$ShellStr = "mysqldump --add-drop-table -u ".DB_USER." ".DB_NAME." ais_piutang >> $FName";
		`$ShellStr`;
		$ShellStr = "mysqldump --add-drop-table -u ".DB_USER." ".DB_NAME." ais_hutang >> $FName";
		`$ShellStr`;

		//ZIP Database
		$ShellStr = "zip -jm ".BACKUP_DIR."/Backup_ALLTRANSACTIONDB_".date("Y-m-d").".zip"." $FName";
		`$ShellStr`;

		header("Location: $FileName");
		exit;		 
	 }
 }

//Initiate Database Table For Search;
$backuptype = GetParam("backuptype","");
$fromDate = GetParam("fromDate","");
$toDate   = GetParam("toDate","");
$periodDay = GetParam("periodDay","");
//******* DATABASE FORM QUERY ***********

$SQL = "SELECT * FROM `$TableName` ";

$SQLCount = "SELECT count(*) FROM `$TableName` ";

//Retrieve Parameter from search
if ($FCode) {
	$CustomWhere = " user like '".$FCode."%'";
	if (strlen($WHERE)) $WHERE .= " AND ".$CustomWhere;
	else $WHERE = $CustomWhere;	
}

if ($FName) {
	$CustomWhere = " $TableName.group_id = '".$FName."'";
	if (strlen($WHERE)) $WHERE .= " AND ".$CustomWhere;
	else $WHERE = $CustomWhere;
}

//Build  WHERE 
if (strlen($WHERE))	$WHERE = " WHERE ".$WHERE;
else $WHERE = " WHERE 1";

$SQL = $SQL.$WHERE;
//print $SQL;
//Select User Group

//******* SHOW FORM CONTENT *********
$PageNum = 1;
$FileList = ReadFileList(BACKUP_DIR);
if ($FileList) asort($FileList);

$RecCnt = count($FileList);

if (GetParam("PageNum","")) $PageNum  = GetParam("PageNum","");

$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);
$TBS->MergeBlock("bln",$KEY);
$TBS->MergeBlock("blg",$FileList,'',$PageSize,$PageNum,$RecCnt);

$TBS->MergeBlock("monthformblk",GetArrayMonth());
$TBS->MergeBlock("yearformblk",GetArrayYear());
$TBS->MergeNavigationBar('nv',array('size'=>10,'pos'=>'centred'),$PageNum,$RecCnt,$PageSize) ; 
$TBS->Show();

?>
